package com.ls.search;

/**
 * 插值插值
 */
public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i;
        }
        InsertValueSearch insertValueSearch = new InsertValueSearch();
        int result = insertValueSearch.search(arr, 0, arr.length-1, 1);
        System.out.println(result);

    }


    public int search(int[] arr, int left, int right, int value) {
        System.out.println("***");
        if (left > right || value < arr[left] || value > arr[right]) {
            return -1;
        }
        //自适应
        int mid = left + (right - left) * (value - arr[left]) / (arr[right] - arr[left]);

        int midval = arr[mid];

        if (value < midval) {
            return search(arr, left, mid - 1, value);
        } else if (value > midval) {
            return search(arr, mid + 1, right, value);
        } else {
            return mid;
        }
    }
}
